package cn.stylefeng.guns.modular.life.controller;

import cn.stylefeng.roses.core.base.controller.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;

import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;

import cn.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import cn.stylefeng.guns.modular.system.model.Lifetype;
import cn.stylefeng.guns.modular.system.model.Medicaltype;
import cn.stylefeng.guns.modular.life.service.ILifetypeService;

/**
 * 生活期刊控制器
 *
 * @author fengshuonan
 * @Date 2020-06-20 23:44:21
 */
@Controller
@RequestMapping("/lifetype")
public class LifetypeController extends BaseController {

    private String PREFIX = "/life/lifetype/";

    @Autowired
    private ILifetypeService lifetypeService;

    /**
     * 跳转到生活期刊首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "lifetype.html";
    }

    /**
     * 跳转到添加生活期刊
     */
    @RequestMapping("/lifetype_add")
    public String lifetypeAdd() {
        return PREFIX + "lifetype_add.html";
    }

    /**
     * 跳转到修改生活期刊
     */
    @RequestMapping("/lifetype_update/{lifetypeId}")
    public String lifetypeUpdate(@PathVariable Integer lifetypeId, Model model) {
        Lifetype lifetype = lifetypeService.selectById(lifetypeId);
        model.addAttribute("item",lifetype);
        LogObjectHolder.me().set(lifetype);
        return PREFIX + "lifetype_edit.html";
    }

    /**
     * 获取生活期刊列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
    	
    	EntityWrapper<Lifetype> wrapper = new EntityWrapper<Lifetype>();
    	wrapper.like("goods_name", condition).or()
    	.like("id", condition);
    	
        return lifetypeService.selectList(wrapper);
    }

    /**
     * 新增生活期刊
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Lifetype lifetype) {
        lifetypeService.insert(lifetype);
        return SUCCESS_TIP;
    }

    /**
     * 删除生活期刊
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer lifetypeId) {
        lifetypeService.deleteById(lifetypeId);
        return SUCCESS_TIP;
    }

    /**
     * 修改生活期刊
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Lifetype lifetype) {
        lifetypeService.updateById(lifetype);
        return SUCCESS_TIP;
    }

    /**
     * 生活期刊详情
     */
    @RequestMapping(value = "/detail/{lifetypeId}")
    @ResponseBody
    public Object detail(@PathVariable("lifetypeId") Integer lifetypeId) {
        return lifetypeService.selectById(lifetypeId);
    }
    @InitBinder
   	public void initBinder(WebDataBinder binder) {
   	    DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
   	    binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));        
   	}
}
